package org.spl.elevator.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PasswordPanel extends JPanel implements ActionListener{

	private ButtonEl buttons[] = new ButtonEl[12];
	//private JPasswordField passwordField = new JPasswordField();
	private JTextField passwordField = new JTextField();
	private String password = "";
	private boolean pressedEnter = false;
	
	public PasswordPanel(){
		this.setLayout(new GridBagLayout());
		
		GridBagConstraints c1 = new GridBagConstraints();
		c1.fill = GridBagConstraints.HORIZONTAL;
		
		passwordField.setBackground(java.awt.Color.BLACK);
		passwordField.setForeground(java.awt.Color.WHITE);
		passwordField.setEditable(false);
		passwordField.setEnabled(false);
		Font font = new Font("Courier", Font.BOLD,12);
		passwordField.setFont(font);
		passwordField.setHorizontalAlignment(JTextField.CENTER);
		
		c1.fill = GridBagConstraints.HORIZONTAL;
		c1.ipady = 12;      //make this component tall
		c1.weightx = 0.0;
		c1.gridwidth = 3;
		c1.gridx = 0;
		c1.gridy = 0;
		this.add(passwordField, c1);
		
		
		GridBagConstraints c2 = new GridBagConstraints();
		c2.fill = GridBagConstraints.HORIZONTAL;
		
		int linha = 1;
		int coluna = 0;
		
		for(int i = 1; i < 10; i++){
			buttons[i] = new ButtonEl(""+i+"");
			buttons[i].setBackground(java.awt.Color.GREEN);
			buttons[i].addActionListener(this);
			c2.fill = GridBagConstraints.HORIZONTAL;
			c2.ipadx = 50;
			c2.weightx = 0.5;
			
			if(coluna == 3){
				coluna = 0;
				linha++;
			}
			
			c2.gridx = coluna;		
			c2.gridy = linha;
			this.add(buttons[i], c2);
			
			coluna++;
		}
		
		buttons[10] = new ButtonEl("ENTER");
		buttons[10].setBackground(java.awt.Color.GREEN);
		buttons[10].addActionListener(this);
		c2.fill = GridBagConstraints.HORIZONTAL;
		c2.weightx = 0.5;
		c2.gridx = 0;		
		c2.gridy = 4;
		this.add(buttons[10], c2);
		
		buttons[0] = new ButtonEl("0");
		buttons[0].setBackground(java.awt.Color.GREEN);
		buttons[0].addActionListener(this);
		c2.fill = GridBagConstraints.HORIZONTAL;
		c2.weightx = 0.5;
		c2.gridx = 1;		
		c2.gridy = 4;
		this.add(buttons[0], c2);
		
		buttons[11] = new ButtonEl("CANCEL");
		buttons[11].setBackground(java.awt.Color.GREEN);
		buttons[11].addActionListener(this);
		c2.fill = GridBagConstraints.HORIZONTAL;
		c2.weightx = 0.5;
		c2.gridx = 2;		
		c2.gridy = 4;
		this.add(buttons[11], c2);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().substring(0).equals("ENTER")){
			pressedEnter = true;
		} else if(e.getActionCommand().substring(0).equals("CANCEL")){
			password = "";
			passwordField.setText(password);
			pressedEnter = false;
		} else {
			int who = Integer.valueOf(e.getActionCommand().substring(0)).intValue();
			passwordField.setText(passwordField.getText()+"*");
			password = password+""+who;
		}
	}
	
	public String getPassword(){
		return password;
	}
	
	public void setPassword(String text){
		password = text;
	}
	
	public void setPasswordField(String text){
		passwordField.setText(text);
	}
	
	public boolean pressedEnter(){
		return pressedEnter;
	}
	
	public void setPressedEnter(boolean pressed){
		pressedEnter = pressed;
	}
}
